<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 17:11
 */
namespace app\index\controller;

use app\index\model\Tea as TeaModel;
use think\Db;

class Tea extends Common
{
	//用于存储实例化TeaModel;
	protected $tea = '';
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
		//实例化TeaMedel
		$this->tea = new TeaModel();
	}
	
	/**
	 * 添加茶页面
	 * @param string get.sid 线下店id
	*/
	public function add()
	{
		$sid = input('get.sid');
		return view('add',compact('sid'));
	}
	
	/**
	 * @param string post.name 茶名
	 * @param string post.largeprice 大杯价格
	 * @param string post.inprice 中杯价格
	 * @param string post.smallprice 小杯价格
	 * @param string post.newimage 新图片
	*/
	public function insert()
	{
		if(request()->isAjax()){
			try{
				$input['name'] = input('post.name');
				$input['sid'] = input('post.sid');
				$input['largeprice'] = input('post.largeprice');
				$input['inprice'] = input('post.inprice');
				$input['smallprice'] = input('post.smallprice');
				$input['image'] = input('post.newimage');
				$result = $this->tea->insertGetId($input);
				if(!$result){
					throw new \PDOException('添加失败');
				}
				$success = ['success'=>'添加成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 首页
	 * @param string get.id 店id
	*/
	public function index()
	{
		$id = input('get.id');
		return view('index',compact('id'));
	}
	
	/**
	 * 列表
	 * @param string get.rows 每页显示的条数
	 * @param string get.page 第几页
	 * @param string get.titles 要检索的字段
	*/
	public function info()
	{
		$where = [];
		$sid = input('get.sid');
		$rows = input('get.rows');
		$page = input('get.page');
		if(!empty(input('get.titles'))){
			$title = input('get.titles');
			$where['name'] = ['like',"%{$title}%"];
			//$where['price'] = ['like',"%{$title}%"];
		}
		//结算总条数
		$total = $this->tea->whereOr($where)->where('sid',$sid)->where('status',1)->count();
		//查询信息
		$rows = $this->tea->whereOr($where)->where('sid',$sid)->where('status',1)
			->order("id desc")->limit($rows)->page($page)->select();
		//分页时需要获取记录总数，键值为 total
		$result["total"] = $total;
		//获取的记录
		$result["rows"] = $rows;
		return json($result);
	}
	
	/**
	 * 编辑
	 * @param string get.id id号
	*/
	public function edit()
	{
		$id = input('get.id');
		$result = $this->tea->where('id',$id)->find();
		return view('edit',compact('id','result'));
	}
	
	/**
	 * 跟新
	 * @param string post.id id号
	 * @param string post.name 茶名
	 * @param string post.largeprice 大杯价格
	 * @param string post.inprice 中杯价格
	 * @param string post.smallprice 小杯价格
	 * @param string post.newimage 新图片
	*/
	public function update()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$input['name'] = input('post.name');
				$input['largeprice'] = input('post.largeprice');
				$input['inprice'] = input('post.inprice');
				$input['smallprice'] = input('post.smallprice');
				if(!empty(input('post.newimage'))){
					$input['image'] = input('post.newimage');
				}
				$result = $this->tea->where('id',$id)->update($input);
				if(!$result){
					throw new \PDOException('修改失败');
				}
				$success = ['success'=>'修改成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 茶图片上传
	 * @param string get.id id号
	 * @param array request()->file('文件名')
	*/
	public function upload()
	{
		try{
			if(request()->file('file')){
				$teaImage = request()->file('file');
				$teaPath = ROOT_PATH."public".DS.'tea';
				$info = $teaImage->move($teaPath);
				if(!$info){
					throw new \PDOException($teaImage->getError());
				}
				$resImage = 'tea'.DS.$info->getSaveName();
				$success = ['success'=>$resImage];
				return json($success);
			}
		}catch (\PDOException $e){
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	
	}
	
	/**
	 * 删除
	 * @param string post.id id号
	*/
	public function del()
	{
		try{
			$id = input('post.id');
			$result = $this->tea->where('id',$id)->update(['status'=>2]);
			if(!$result){
				throw new \PDOException('删除失败');
			}
			$success = ['success'=>'删除成功'];
			return json($success);
		}catch (\PDOException $e){
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	
	/**
	 * 购买信息
	 * @param string post.id id号
	*/
	public function buyinfo()
	{
		try{
			$id = input('post.id');
			$num = input('post.num');
			$result = $this->tea->where('id',$id)->find();
			if(!$result){
				throw new \PDOException('没有此茶信息');
			}
			$info = ['info'=>$result,'num'=>$num];
			return json($info);
		}catch (\PDOException $e){
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
}